package common.comp
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.LButton;

	/**
	 *按钮组 可选择 
	 * @author lake
	 * 
	 */	
	public class TabButtonBar extends Sprite
	{
		/**
		 *选择类型 
		 */		
		public var selectType 		:int = 0;
		/**	
		 *选择的btn 
		 */		
		public var selectBtn 		:LButton;
		/**
		 *按钮数组
		 * LButton 
		 */		
		private var btnArr 		:Array;
		/**
		 *按钮状态 
		 */		
		public var isButtonMode  	:Boolean = true;
		
		public function TabButtonBar()
		{
			super();
		}
		/**
		 *设置按钮 
		 * @param btns
		 * 
		 */		
		public function setButtons(btns :Array):void
		{
			btnArr = btns;
			for each(var lbtn :LButton in btnArr)
			{
				setButtonMode(lbtn);
				lbtn.addEventListener(MouseEvent.CLICK,clickHandler);
			}
		}
		/**
		 *点击捕获 
		 * @param e
		 * 
		 */		
		private function clickHandler(e :MouseEvent):void
		{
			selectTabButton( e.target as LButton ); 
		}
		/**
		 *选择某个tab button 
		 * @param lBtn
		 * 
		 */		
		public function selectTabButton(lBtn :LButton):void
		{
			if(selectBtn)
			{
				selectBtn.enable = true;
				selectBtn.setStatus(LButton.UP);
			}
			selectBtn = lBtn;
			setSelectBtnStatus();
		}
		/**
		 *选择某个tab button 通过index 
		 * @param index
		 * 
		 */		
		public function setTabButtonByIndex(index :int):void
		{
			if(selectBtn)
			{
				selectBtn.enable = true;
				selectBtn.setStatus(LButton.UP);
			}
			selectBtn = btnArr[index];
			setSelectBtnStatus();
		}
		/**
		 * 设置选择选项的状态
		 */		
		private function setSelectBtnStatus():void
		{
			if(selectBtn)
			{
				selectBtn.enable = false;
				selectBtn.setStatus(LButton.SELECT);
				selectType = btnArr.indexOf(selectBtn);
				this.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		/**
		 *设置鼠标手型 
		 * 
		 */		
		private function setButtonMode(selBtn :LButton):void
		{
			if(isButtonMode)
			{
				selBtn.buttonMode = true;
			}
			else
			{
				selBtn.buttonMode = false;
			}
		}
		/**
		 *销毁 
		 */		
		public function destroyView():void
		{
			selectBtn = null;
			for each(var lbtn :LButton in btnArr)
			{
				lbtn.removeEventListener(MouseEvent.CLICK,clickHandler);
			}
			btnArr = null;
		}
	}
}